package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.NoProcessing;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourceVo> list(ResourceDto resourceDto) {
        Resource resource = BeanUtil.toBean(resourceDto,Resource.class);
        List<ResourceVo> list = resourceMapper.list(resource);
        return list;
    }

    @Override
    public TreeVo tree(ResourceDto resourceDto){
        //查询所有菜单数据
        Resource resource = BeanUtil.toBean(resourceDto,Resource.class);
        resource.setResourceType("m");
        resource.setDataState("0");

        List<ResourceVo> listResources = resourceMapper.list(resource);

        //装所有数据
        List<TreeItemVo> treeItemVos = new ArrayList<>();

        //根目录
        ResourceVo root = new ResourceVo();
        root.setResourceNo("100001000000000");
        root.setResourceName("智慧养老院");

        //从一级一下开始递归
        recursionTree(treeItemVos,root,listResources);

        TreeVo treeVo = new TreeVo();
        treeVo.setItems(treeItemVos);
        return treeVo;
    }

    @Override
    public void addResource(ResourceDto resourceDto) {
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        //1.查询父资源的数据
        Resource res = resourceMapper.getResource(resource.getParentResourceNo());
        resource.setDataState(res.getDataState());

        //2.根据父资源菜单 去创建子级菜单
        String resourceNo = createResourceNo(resource.getParentResourceNo());

        resource.setResourceNo(resourceNo);
        resourceMapper.insert(resource);
    }

    private String createResourceNo(String parentResourceNo) {
        //1. 判断父菜单是否大于三级
        //100 001 001 001 001
        if (NoProcessing.processString(parentResourceNo).length()/3 >= 5){
            throw new BaseException(BasicEnum.RESOURCE_DEPTH_UPPER_LIMIT);
        }
        //2.查询父级下面是否包含子集
        Resource resource = new Resource();
        resource.setParentResourceNo(parentResourceNo);
        List<ResourceVo> listResources = resourceMapper.list(resource);
        //查询父类没有数据，没有子集
        if (ObjectUtil.isEmpty(listResources)){
            //添加新的子集
            return NoProcessing.createNo(parentResourceNo,false);
        }
        //查询数据，子集菜单有多个，需要获取最大的那个，+1
        Long max = listResources.stream().mapToLong(r -> Long.parseLong(r.getResourceNo())).max().orElse(0);
        NoProcessing.createNo(String.valueOf(max),true);

        /*Long m = listResources.stream().map(r -> {
            return Long.valueOf(r.getResourceNo());
        }).max(Comparator.comparing(a -> a)).get();*/
        return NoProcessing.createNo(String.valueOf(max),true);

    }

    @Override
    public void updateResource(ResourceDto resourceDto) {
        resourceMapper.updateResource(resourceDto);
    }

    @Override
    public void deleteResource(String resourceNo) {
        //判断是否有子级菜单
        String sonRes = resourceMapper.getSonResour(resourceNo);
        if (BeanUtil.isNotEmpty(sonRes)){
            throw new BaseException(BasicEnum.USER_LOCATED_BOTTOMED_DEPT);
        }
        resourceMapper.deleteResource(resourceNo);
    }

    @Override
    public Object ResourceStatus(ResourceDto resourceDto) {
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        //判断父资源是否禁用
        ResourceVo parent = resourceMapper.getParent(resource.getParentResourceNo());
        if (parent.getDataState().equals("1") && resource.getDataState().equals("0"))
            throw new BaseException(BasicEnum.PARENT_DEPT_DISABLE);
        //禁用子资源
        if (BeanUtil.isNotEmpty(resourceMapper.getSonResour(resource.getResourceNo()))){
            String enable = NoProcessing.processString(resourceDto.getResourceNo());
            resourceMapper.resourceEnable(resource.getDataState(),enable);
        }else
            resourceMapper.resourceEnable(resource.getDataState(),resource.getResourceNo());

        return true;
    }


    private void recursionTree(List<TreeItemVo> treeItemVos, ResourceVo root, List<ResourceVo> listResources) {
        //先构建根目录对象
        TreeItemVo itemVo = new TreeItemVo();
        itemVo.setId(root.getResourceNo());
        itemVo.setLabel(root.getResourceName());

        //构建根目录楼下的以及菜单
        List<ResourceVo> childlist = listResources
                .stream().
                filter(r -> r.getParentResourceNo().equals(root.getResourceNo()))
                .collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(childlist)){
            //递归使用的子集对象
            List<TreeItemVo> children = new ArrayList<>();
            //递归的2级，3级数据
            childlist.forEach(res -> {
                recursionTree(children,res,listResources);
            });

            //设置子集child属性
            itemVo.setChildren(children);
        }
        //item 集合添加菜单对象
        treeItemVos.add(itemVo);
    }

}




























